<?php
namespace App\Services;

/**
 * 图文分析数据服务类
 * @package App\Services
 */
class WxtjArticle
{
    /**
     * 所有统计数据开始日期
     * @var string
     */
    private $allBeginDate;
    /**
     * 今天日期
     * @var false|string
     */
    private $todayDate;

    /**
     * 构造函数
     */
    public function __construct()
    {
        //全部拉取开始日期
        //$this->allBeginDate = '2014-12-01';
        $this->allBeginDate = date('Y').'-01-01';
        $this->todayDate = date('Y-m-d');
    }

    /**
     * 图文群发每日数据拉取
     * @return bool
     */
    public function pullAllSummary()
    {
        $beginDate = $this->allBeginDate;
        while ($beginDate < $this->todayDate){
            $endDate = $beginDate;
            if($endDate >= $this->todayDate ){
                $endDate = date('Y-m-d', strtotime($this->todayDate .' -1 days'));
            }
            //异步处理统计数据拉取
            \App\Jobs\WxtjArticleSummaryHandle::dispatch($beginDate, $endDate)->onQueue('low');
            if($endDate >= $this->todayDate ){
                $beginDate = date('Y-m-d', strtotime($endDate . '+2 days'));
            }else{
                $beginDate = date('Y-m-d', strtotime($endDate. ' +1 days'));
            }
        };
        return true;
    }
    /**
     * 图文群发每日数据拉取
     * @param string $beginDate
     * @param string $endDate
     * @return bool
     * @throws \Exception
     */
    public function pullSummary($beginDate = '', $endDate = '')
    {
        $beginDate || $beginDate = date('Y-m-d', strtotime('-1 days'));
        $endDate || $endDate = date('Y-m-d', strtotime('-1 days'));
        $app = \App\Services\EasyWechat::officialAccount();
        $apiRs = $app->data_cube->articleSummary($beginDate, $endDate);
        WxErrorService::checkApiResponse($apiRs);
        if (!empty($apiRs['list'])) {
            foreach ($apiRs['list'] as $v) {
                \App\Models\WxtjArticleSummary::updateOrCreate(
                    [
                        'msgid' => $v['msgid'],
                        'ref_date' => $v['ref_date'],
                    ],
                    [
                        'title' => $v['title'],
                        'int_page_read_user' => $v['int_page_read_user'] ?? 0,
                        'int_page_read_count' => $v['int_page_read_count'] ?? 0,
                        'ori_page_read_user' => $v['ori_page_read_user'] ?? 0,
                        'ori_page_read_count' => $v['ori_page_read_count'] ?? 0,
                        'share_user' => $v['share_user'] ?? 0,
                        'share_count' => $v['share_count'] ?? 0,
                        'add_to_fav_user' => $v['add_to_fav_user'] ?? 0,
                        'add_to_fav_count' => $v['add_to_fav_count'] ?? 0,
                    ]
                );
            }
        }
        return true;
    }

    /**
     * 拉取所有图文群发总数据
     * @return bool
     */
    public function pullAllTotal()
    {
        $beginDate = $this->allBeginDate;
        while ($beginDate < $this->todayDate){
            $endDate = $beginDate;
            if($endDate >= $this->todayDate ){
                $endDate = date('Y-m-d', strtotime($this->todayDate .' -1 days'));
            }
            //异步处理统计数据拉取
            \App\Jobs\WxtjArticleTotalHandle::dispatch($beginDate, $endDate)->onQueue('low');
            if($endDate >= $this->todayDate ){
                $beginDate = date('Y-m-d', strtotime($endDate . '+2 days'));
            }else{
                $beginDate = date('Y-m-d', strtotime($endDate. ' +1 days'));
            }
        };
        return true;
    }
    /**
     * 图文群发总数据拉取
     * @param string $beginDate
     * @param string $endDate
     * @return bool
     * @throws \Exception
     */
    public function pullTotal($beginDate = '', $endDate = ''){
        $beginDate || $beginDate = date('Y-m-d', strtotime('-1 days'));
        $endDate || $endDate = date('Y-m-d', strtotime('-1 days'));
        $app = \App\Services\EasyWechat::officialAccount();
        $apiRs = $app->data_cube->articleTotal($beginDate, $endDate);
        WxErrorService::checkApiResponse($apiRs);
        if (!empty($apiRs['list'])) {
            foreach ($apiRs['list'] as $article) {
                if (!empty($article['details']) && is_array($article['details'])){
                    foreach ($article['details'] as $v){
                        \App\Models\WxtjArticleTotal::updateOrCreate(
                            [
                                'msgid' => $article['msgid'],
                                'ref_date' => $article['ref_date'],
                                'stat_date' => $v['stat_date'],
                            ],
                            [
                                'title' => $article['title'],
                                'target_user' => $v['target_user'],
                                'int_page_read_user' => $v['int_page_read_user'] ?? 0,
                                'int_page_read_count' => $v['int_page_read_count'] ?? 0,
                                'ori_page_read_user' => $v['ori_page_read_user'] ?? 0,
                                'ori_page_read_count' => $v['ori_page_read_count'] ?? 0,
                                'share_user' => $v['share_user'] ?? 0,
                                'share_count' => $v['share_count'] ?? 0,
                                'add_to_fav_user' => $v['add_to_fav_user'] ?? 0,
                                'add_to_fav_count' => $v['add_to_fav_count'] ?? 0,
                                'int_page_from_session_read_user' => $v['int_page_from_session_read_user'] ?? 0,
                                'int_page_from_session_read_count' => $v['int_page_from_session_read_count'] ?? 0,
                                'int_page_from_hist_msg_read_user' => $v['int_page_from_hist_msg_read_user'] ?? 0,
                                'int_page_from_hist_msg_read_count' => $v['int_page_from_hist_msg_read_count'] ?? 0,
                                'int_page_from_feed_read_user' => $v['int_page_from_feed_read_user'] ?? 0,
                                'int_page_from_feed_read_count' => $v['int_page_from_feed_read_count'] ?? 0,
                                'int_page_from_friends_read_user' => $v['int_page_from_friends_read_user'] ?? 0,
                                'int_page_from_friends_read_count' => $v['int_page_from_friends_read_count'] ?? 0,
                                'int_page_from_other_read_user' => $v['int_page_from_other_read_user'] ?? 0,
                                'int_page_from_other_read_count' => $v['int_page_from_other_read_count'] ?? 0,
                                'int_page_from_kanyikan_read_user' => $v['int_page_from_kanyikan_read_user'] ?? 0,
                                'int_page_from_kanyikan_read_count' => $v['int_page_from_kanyikan_read_count'] ?? 0,
                                'int_page_from_souyisou_read_user' => $v['int_page_from_souyisou_read_user'] ?? 0,
                                'int_page_from_souyisou_read_count' => $v['int_page_from_souyisou_read_count'] ?? 0,
                                'feed_share_from_session_user' => $v['feed_share_from_session_user'] ?? 0,
                                'feed_share_from_session_cnt' => $v['feed_share_from_session_cnt'] ?? 0,
                                'feed_share_from_feed_user' => $v['feed_share_from_feed_user'] ?? 0,
                                'feed_share_from_feed_cnt' => $v['feed_share_from_feed_cnt'] ?? 0,
                                'feed_share_from_other_user' => $v['feed_share_from_other_user'] ?? 0,
                                'feed_share_from_other_cnt' => $v['feed_share_from_other_cnt'] ?? 0,
                            ]
                        );
                    }
                }
            }
        }
        return true;
    }

    /**
     * 拉取所有图文统计数据
     * @return bool
     */
    public function pullAllUserRead()
    {
        $beginDate = $this->allBeginDate;
        while ($beginDate < $this->todayDate){
            $endDate = date('Y-m-d', strtotime($beginDate.' +2 days'));
            if($endDate >= $this->todayDate ){
                $endDate = date('Y-m-d', strtotime($this->todayDate .' -1 days'));
            }
            //异步处理统计数据拉取
            \App\Jobs\WxtjArticleUserreadHandle::dispatch($beginDate, $endDate)->onQueue('low');
            if($endDate >= $this->todayDate ){
                $beginDate = date('Y-m-d', strtotime($endDate . '+2 days'));
            }else{
                $beginDate = date('Y-m-d', strtotime($endDate. ' +1 days'));
            }
        };
        return true;
    }
    /**
     * 图文统计数据拉取
     * @param string $beginDate
     * @param string $endDate
     * @return bool
     * @throws \Exception
     */
    public function pullUserRead($beginDate = '', $endDate = ''){
        $beginDate || $beginDate = date('Y-m-d', strtotime('-3 days'));
        $endDate || $endDate = date('Y-m-d', strtotime('-1 days'));
        $app = \App\Services\EasyWechat::officialAccount();
        $apiRs = $app->data_cube->userReadSummary($beginDate, $endDate);
        WxErrorService::checkApiResponse($apiRs);
        if (!empty($apiRs['list'])) {
            foreach ($apiRs['list'] as $v) {
                \App\Models\WxtjArticleUserread::updateOrCreate(
                    [
                        'ref_date' => $v['ref_date'],
                    ],
                    [
                        'int_page_read_user' => $v['int_page_read_user'] ?? 0,
                        'int_page_read_count' => $v['int_page_read_count'] ?? 0,
                        'ori_page_read_user' => $v['ori_page_read_user'] ?? 0,
                        'ori_page_read_count' => $v['ori_page_read_count'] ?? 0,
                        'share_user' => $v['share_user'] ?? 0,
                        'share_count' => $v['share_count'] ?? 0,
                        'add_to_fav_user' => $v['add_to_fav_user'] ?? 0,
                        'add_to_fav_count' => $v['add_to_fav_count'] ?? 0,
                    ]
                );
            }
        }
        return true;
    }

    /**
     * 拉取所有图文统计分时数据
     * @return bool
     */
    public function pullAllUserReadHour()
    {
        $beginDate = $this->allBeginDate;
        while ($beginDate < $this->todayDate){
            $endDate = $beginDate;
            if($endDate >= $this->todayDate ){
                $endDate = date('Y-m-d', strtotime($this->todayDate .' -1 days'));
            }
            //异步处理统计数据拉取
            \App\Jobs\WxtjArticleUserreadhourHandle::dispatch($beginDate, $endDate)->onQueue('low');
            if($endDate >= $this->todayDate ){
                $beginDate = date('Y-m-d', strtotime($endDate . '+2 days'));
            }else{
                $beginDate = date('Y-m-d', strtotime($endDate. ' +1 days'));
            }
        };
        return true;
    }
    /**
     * 图文统计分时数据拉取
     * @param string $beginDate
     * @param string $endDate
     * @return bool
     * @throws \Exception
     */
    public function pullUserReadHour($beginDate = '', $endDate = ''){
        $beginDate || $beginDate = date('Y-m-d', strtotime('-1 days'));
        $endDate || $endDate = date('Y-m-d', strtotime('-1 days'));
        $app = \App\Services\EasyWechat::officialAccount();
        $apiRs = $app->data_cube->userReadHourly($beginDate, $endDate);
        WxErrorService::checkApiResponse($apiRs);
        if (!empty($apiRs['list'])) {
            foreach ($apiRs['list'] as $v) {
                \App\Models\WxtjArticleUserreadhour::updateOrCreate(
                    [
                        'ref_date' => $v['ref_date'],
                        'ref_hour' => $v['ref_hour'],
                        'user_source' => $v['user_source'],
                    ],
                    [
                        'int_page_read_user' => $v['int_page_read_user'] ?? 0,
                        'int_page_read_count' => $v['int_page_read_count'] ?? 0,
                        'ori_page_read_user' => $v['ori_page_read_user'] ?? 0,
                        'ori_page_read_count' => $v['ori_page_read_count'] ?? 0,
                        'share_user' => $v['share_user'] ?? 0,
                        'share_count' => $v['share_count'] ?? 0,
                        'add_to_fav_user' => $v['add_to_fav_user'] ?? 0,
                        'add_to_fav_count' => $v['add_to_fav_count'] ?? 0,
                    ]
                );
            }
        }
        return true;
    }

    /**
     * 拉取所有图文分享转发数据
     * @return bool
     */
    public function pullAllUserShare()
    {
        $beginDate = $this->allBeginDate;
        while ($beginDate < $this->todayDate){
            $endDate = date('Y-m-d', strtotime($beginDate . ' +6 days'));
            if($endDate >= $this->todayDate ){
                $endDate = date('Y-m-d', strtotime($this->todayDate .' -1 days'));
            }
            //异步处理统计数据拉取
            \App\Jobs\WxtjArticleUsershareHandle::dispatch($beginDate, $endDate)->onQueue('low');
            if($endDate >= $this->todayDate ){
                $beginDate = date('Y-m-d', strtotime($endDate . '+2 days'));
            }else{
                $beginDate = date('Y-m-d', strtotime($endDate. ' +1 days'));
            }
        };
        return true;
    }
    /**
     * 图文分享转发数据拉取
     * @param string $beginDate
     * @param string $endDate
     * @return bool
     * @throws \Exception
     */
    public function pullUserShare($beginDate = '', $endDate = ''){
        $beginDate || $beginDate = date('Y-m-d', strtotime('-7 days'));
        $endDate || $endDate = date('Y-m-d', strtotime('-1 days'));
        $app = \App\Services\EasyWechat::officialAccount();
        $apiRs = $app->data_cube->userShareSummary($beginDate, $endDate);
        WxErrorService::checkApiResponse($apiRs);
        if (!empty($apiRs['list'])) {
            foreach ($apiRs['list'] as $v) {
                \App\Models\WxtjArticleUsershare::updateOrCreate(
                    [
                        'ref_date' => $v['ref_date'],
                        'share_scene' => $v['share_scene'],
                    ],
                    [
                        'share_count' => $v['share_count'] ?? 0,
                        'share_user' => $v['share_user'] ?? 0,
                    ]
                );
            }
        }
        return true;
    }

    /**
     * 拉取所有图文分享转发分时数据
     * @return bool
     */
    public function pullAllUserShareHour()
    {
        $beginDate = $this->allBeginDate;
        while ($beginDate < $this->todayDate){
            $endDate = $beginDate;
            if($endDate >= $this->todayDate ){
                $endDate = date('Y-m-d', strtotime($this->todayDate .' -1 days'));
            }
            //异步处理统计数据拉取
            \App\Jobs\WxtjArticleUserreadhourHandle::dispatch($beginDate, $endDate)->onQueue('low');
            if($endDate >= $this->todayDate ){
                $beginDate = date('Y-m-d', strtotime($endDate . '+2 days'));
            }else{
                $beginDate = date('Y-m-d', strtotime($endDate. ' +1 days'));
            }
        };
        return true;
    }
    /**
     * 图文分享转发分时数据拉取
     * @param string $beginDate
     * @param string $endDate
     * @return bool
     * @throws \Exception
     */
    public function pullUserShareHour($beginDate = '', $endDate = ''){
        $beginDate || $beginDate = date('Y-m-d', strtotime('-1 days'));
        $endDate || $endDate = date('Y-m-d', strtotime('-1 days'));
        $app = \App\Services\EasyWechat::officialAccount();
        $apiRs = $app->data_cube->userShareHourly($beginDate, $endDate);
        WxErrorService::checkApiResponse($apiRs);
        if (!empty($apiRs['list'])) {
            foreach ($apiRs['list'] as $v) {
                \App\Models\WxtjArticleUsersharehour::updateOrCreate(
                    [
                        'ref_date' => $v['ref_date'],
                        'ref_hour' => $v['ref_hour'],
                        'share_scene' => $v['share_scene'],
                    ],
                    [
                        'share_count' => $v['share_count'] ?? 0,
                        'share_user' => $v['share_user'] ?? 0,
                    ]
                );
            }
        }
        return true;
    }
}